package a51qindian.com.comprehensive.base;

import android.text.TextUtils;

import com.li.base.BaseFragment;
import com.li.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description:
 */

public abstract class QDBaseFragment extends BaseFragment implements OnRefreshLoadmoreListener {
    private Unbinder mUnbinder;
    protected String mUid;

    @Override
    protected void initSth() {
        mUnbinder = ButterKnife.bind(this, getContentView());
        mUid = SpUtil.getUserInfo(mContext, SpUtil.USER_ID);
    }

    protected int mPage = 0;
    protected boolean mIsRefresh = true;
    private SmartRefreshLayout mSmartRefreshLayout;

    protected void setSmartRefreshLayout(SmartRefreshLayout smartRefreshLayout) {
        mSmartRefreshLayout = smartRefreshLayout;
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        cancelAllDisposable();
    }

    private List<Disposable> mDisposableList;

    protected void addDisposable(Disposable disposable) {
        if (mDisposableList == null) {
            mDisposableList = new ArrayList<>();
        }
        mDisposableList.add(disposable);
    }

    protected void cancelAllDisposable() {
        if (mDisposableList == null) {
            return;
        }
        for (Disposable disposable : mDisposableList) {
            if (disposable.isDisposed()) {
                continue;
            }
            disposable.dispose();
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mIsRefresh = false;
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mIsRefresh = true;
    }

    protected void stopRefreshLoad() {
        mSmartRefreshLayout.finishRefresh();
        mSmartRefreshLayout.finishLoadmore();
        mSmartRefreshLayout.setLoadmoreFinished(false);
    }
    protected void showToast(String msg){
        if (!TextUtils.isEmpty(msg)) {
            ToastUtil.showToast(mContext,msg);
        }
    }
}
